package it.unisa.unibox.junit.storage;

import it.unisa.unibox.Storage.DBGestioneProdotti;
import it.unisa.unibox.beans.Prodotto;

import java.sql.SQLException;
import java.util.ArrayList;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DBGestioneProdottiRicercaTest extends TestCase {

	private DBGestioneProdotti db;

	ArrayList<Prodotto> prodList;

	@Override
	@After
	protected void setUp() throws Exception {
		db = DBGestioneProdotti.getInstance();
		prodList = null;
		super.setUp();
	}

	@Override
	@Before
	protected void tearDown() throws Exception {
		System.gc();
		super.tearDown();
	}

	@Test
	public void testRicerca1() {
		try {
			prodList = db.ricercaProdotti("", 2004, 2011, "", "", "", "", "",
					"");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertFalse(prodList.isEmpty());
		for (Prodotto p : prodList) {
			assertNotNull(p);
			// assertNotNull(p.getAbstract_field());
			assertNotNull(p.getLingua());
			// assertNotNull(p.getNote());
			assertNotNull(p.getTipologia());
			assertNotNull(p.getTitolo());
			assertTrue(p.getId() > 0);
			assertTrue(p.getStato() >= 1 && p.getStato() <= 6);
			assertNotNull(p.getResponsabile());

			assertTrue(p.getAnno() >= 2004 && p.getAnno() <= 2011);
		}
	}

	@Test
	public void testRicerca2() {
		try {
			prodList = db.ricercaProdotti("libro", 2006, 2011, "", "", "", "",
					"", "");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertFalse(prodList.isEmpty());
		for (Prodotto p : prodList) {
			assertNotNull(p);
			// assertNotNull(p.getAbstract_field());
			assertNotNull(p.getLingua());
			// assertNotNull(p.getNote());
			assertNotNull(p.getTipologia());
			assertNotNull(p.getTitolo());
			assertTrue(p.getId() > 0);
			assertTrue(p.getStato() >= 1 && p.getStato() <= 6);
			assertNotNull(p.getResponsabile());

			assertTrue(p.getAnno() >= 2006 && p.getAnno() <= 2011);
			assertTrue(p.getTipologia().contains("Libro"));
		}
	}

	@Test
	public void testRicerca3() {
		try {
			prodList = db.ricercaProdotti("Monografia", 2000, 2014, "", "", "",
					"", "", "");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertFalse(prodList.isEmpty());
		for (Prodotto p : prodList) {
			assertNotNull(p);
			// assertNotNull(p.getAbstract_field());
			assertNotNull(p.getLingua());
			// assertNotNull(p.getNote());
			assertNotNull(p.getTipologia());
			assertNotNull(p.getTitolo());
			assertTrue(p.getId() > 0);
			assertTrue(p.getStato() >= 1 && p.getStato() <= 6);
			assertNotNull(p.getResponsabile());

			assertTrue(p.getAnno() >= 2000 && p.getAnno() <= 2014);
			assertTrue(p.getTipologia().contains("Monografia"));
		}
	}

	@Test
	public void testRicerca4() {
		try {
			prodList = db.ricercaProdotti("", 2000, 2014, "lib", "", "", "",
					"", "");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertFalse(prodList.isEmpty());
		for (Prodotto p : prodList) {
			assertNotNull(p);
			// assertNotNull(p.getAbstract_field());
			assertNotNull(p.getLingua());
			// assertNotNull(p.getNote());
			assertNotNull(p.getTipologia());
			assertNotNull(p.getTitolo());
			assertTrue(p.getId() > 0);
			assertTrue(p.getStato() >= 1 && p.getStato() <= 6);
			assertNotNull(p.getResponsabile());

			assertTrue(p.getAnno() >= 2000 && p.getAnno() <= 2014);
			assertTrue(p.getTitolo().toLowerCase()
					.contains("lib".toLowerCase()));
		}
	}

	@Test
	public void testRicerca5() {
		try {
			prodList = db.ricercaProdotti("", 2000, 2014, "", "mario", "rossi",
					"", "", "");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertFalse(prodList.isEmpty());
		for (Prodotto p : prodList) {
			assertNotNull(p);
			// assertNotNull(p.getAbstract_field());
			assertNotNull(p.getLingua());
			// assertNotNull(p.getNote());
			assertNotNull(p.getTipologia());
			assertNotNull(p.getTitolo());
			assertTrue(p.getId() > 0);
			assertTrue(p.getStato() >= 1 && p.getStato() <= 6);
			assertNotNull(p.getResponsabile());

			assertTrue(p.getAnno() >= 2000 && p.getAnno() <= 2014);
			assertTrue(p.getResponsabile().getNome().toLowerCase()
					.contains("mario".toLowerCase()));
			assertTrue(p.getResponsabile().getCognome().toLowerCase()
					.contains("rossi".toLowerCase()));
		}
	}

	@Test
	public void testRicerca6() {
		try {
			prodList = db.ricercaProdotti("", 2000, 2014, "", "", "", "", "1",
					"");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertFalse(prodList.isEmpty());
		for (Prodotto p : prodList) {
			assertNotNull(p);
			// assertNotNull(p.getAbstract_field());
			assertNotNull(p.getLingua());
			// assertNotNull(p.getNote());
			assertNotNull(p.getTipologia());
			assertNotNull(p.getTitolo());
			assertTrue(p.getId() > 0);
			assertTrue(p.getStato() >= 1 && p.getStato() <= 6);
			assertNotNull(p.getResponsabile());

			assertTrue(p.getAnno() >= 2000 && p.getAnno() <= 2014);
			assertTrue(p.getResponsabile().getDipartimento().getId() == 1);
		}
	}

	@Test
	public void testRicerca7() {
		try {
			prodList = db.ricercaProdotti("", 2000, 2014, "", "", "", "", "",
					"1");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertFalse(prodList.isEmpty());
		for (Prodotto p : prodList) {
			assertNotNull(p);
			// assertNotNull(p.getAbstract_field());
			assertNotNull(p.getLingua());
			// assertNotNull(p.getNote());
			assertNotNull(p.getTipologia());
			assertNotNull(p.getTitolo());
			assertTrue(p.getId() > 0);
			assertTrue(p.getStato() >= 1 && p.getStato() <= 6);
			assertNotNull(p.getResponsabile());
			
			assertTrue(p.getAnno() >= 2000 && p.getAnno() <= 2014);
			assertTrue(p.getResponsabile().getAreaScientifica().getId() == 1);
		}
	}

	@Test
	public void testRicerca8() {
		try {
			prodList = db.ricercaProdotti("", 2000, 2014, "", "", "", "", "1",
					"6");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertFalse(prodList.isEmpty());
		for (Prodotto p : prodList) {
			assertNotNull(p);
			// assertNotNull(p.getAbstract_field());
			assertNotNull(p.getLingua());
			// assertNotNull(p.getNote());
			assertNotNull(p.getTipologia());
			assertNotNull(p.getTitolo());
			assertTrue(p.getId() > 0);
			assertTrue(p.getStato() >= 1 && p.getStato() <= 6);
			assertNotNull(p.getResponsabile());
			
			assertTrue(p.getAnno() >= 2000 && p.getAnno() <= 2014);
			assertTrue(p.getResponsabile().getDipartimento().getId() == 1);
			assertTrue(p.getResponsabile().getAreaScientifica().getId() == 6);
		}
	}

	@Test
	public void testRicerca9() {
		try {
			prodList = db.ricercaProdotti("", 2004, 2011, "", "", "", "", "",
					"");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertFalse(prodList.isEmpty());
		for (Prodotto p : prodList) {
			assertNotNull(p);
			// assertNotNull(p.getAbstract_field());
			assertNotNull(p.getLingua());
			// assertNotNull(p.getNote());
			assertNotNull(p.getTipologia());
			assertNotNull(p.getTitolo());
			assertTrue(p.getId() > 0);
			assertTrue(p.getStato() >= 1 && p.getStato() <= 6);
			assertNotNull(p.getResponsabile());
			
			assertTrue(p.getAnno() >= 2004 && p.getAnno() <= 2011);
		}
	}

	@Test
	public void testRicerca10() {
		try {
			prodList = db.ricercaProdotti("", 2000, 2014, "", "samuele",
					"bersani", "", "", "");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertFalse(prodList.isEmpty());
		for (Prodotto p : prodList) {
			assertNotNull(p);
			// assertNotNull(p.getAbstract_field());
			assertNotNull(p.getLingua());
			// assertNotNull(p.getNote());
			assertNotNull(p.getTipologia());
			assertNotNull(p.getTitolo());
			assertTrue(p.getId() > 0);
			assertTrue(p.getStato() >= 1 && p.getStato() <= 6);
			assertNotNull(p.getResponsabile());
			
			assertTrue(p.getAnno() >= 2000 && p.getAnno() <= 2014);
			assertTrue(p.getResponsabile().getNome().toLowerCase()
					.contains("samuele".toLowerCase()));
			assertTrue(p.getResponsabile().getCognome().toLowerCase()
					.contains("bersani".toLowerCase()));
		}
	}

	public void testRicercaVuota1() {
		try {
			prodList = db.ricercaProdotti("asd123asd", 2000, 2014, "",
					"samuele", "bersani", "", "", "");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertTrue(prodList.isEmpty());
	}

	public void testRicercaVuota2() {
		try {
			prodList = db.ricercaProdotti("", 2014, 2005, "", "", "", "", "",
					"");
		} catch (SQLException e) {
			fail();
			e.printStackTrace();
		}
		assertNotNull(prodList);
		assertTrue(prodList.isEmpty());
	}
}
